import 'package:flutter/material.dart';
import 'package:flutter_ume/data/model/user_infor_entity.dart';
import 'package:flutter_ume/routers/application.dart';
import 'package:flutter_ume/routers/routes.dart';

import 'package:flutter_ume/common/component_index.dart';

class MinePage extends StatefulWidget {
  @override
  _MinePageState createState() {
    return _MinePageState();
  }
}

class _MinePageState extends State<MinePage>
    with AutomaticKeepAliveClientMixin {
  @override
  bool get wantKeepAlive => true;

  String _userName = '未登录';
  String _userId = '';
  String _userPicUrl = '';
  String _rowTv1 = '0元';
  String _rowTv2 = '0元';
  String _rowTv3 = '~天';
  String _rowTv4 = '~笔';
  String _rowTv5 = '~笔';

  @override
  void initState() {
    super.initState();
    _getNetData();
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);

    return Scaffold(
      appBar: _titleBar(),
      body: _bodyWidget(),
      backgroundColor: Colours.bgColor,
    );
  }

  ///内容
  Widget _bodyWidget() {
    return ListView(
      children: <Widget>[
        _rowHeadAndName(),
        _rowLeadMsg1(),
        _rowLeanMsg2(),
        _tvWifget(),
        _conWidget(),
        _otherWidget(),
        _bottomWidget()
      ],
    );
  }

  Widget _bottomWidget(){
    return Container(
      padding: EdgeInsets.all(8),
      margin: EdgeInsets.fromLTRB(8.0, 0.0, 8.0, 0.0),
      width: ScreenUtil().setWidth(702),
      height: ScreenUtil().setHeight(215),
      color: Colours.cf9,
      child: Column(
        children: <Widget>[
          Text('联众金融 V2.0.1',style: TextStyles.mineTs6,),
          Text('copyright @2018 hitouapp.com inc',style: TextStyles.mineTs6,),
          Text('All Rights Reserved',style: TextStyles.mineTs6,),
          Text('ICP 备 皖B201392-1',style: TextStyles.mineTs6,),
          Text('EDI备 皖C230020-1',style: TextStyles.mineTs6,),
        ],
      ),

    );
  }

  ///其他功能
  Widget _otherWidget() {
    return Container(
        decoration: AppCardStyle.decoration,
        margin: EdgeInsets.fromLTRB(8.0, 0.0, 8.0, 0.0),
        child: Column(
          children: <Widget>[
            Container(
                padding: EdgeInsets.fromLTRB(8.0, 8.0, 0.0, 0.0),
                child: Column(
                  children: <Widget>[
                    Row(
                      children: <Widget>[
                        Expanded(
                          child: Text(
                            '产品介绍',
                            style: TextStyles.mineTs5,
                          ),
                          flex: 1,
                        ),
                        Container(
                          margin: EdgeInsets.fromLTRB(0.0, 0.0, 8.0, 0),
                          child: Image.asset('resource/images/icon_right.png',
                              width: ScreenUtil().setWidth(30),
                              height: ScreenUtil().setHeight(40)),
                        )
                      ],
                    ),
                    Container(
                      margin: EdgeInsets.fromLTRB(0.0, 8.0, 2.0, 0.0),
                      color: Colours.bgec,
                      height: ScreenUtil().setHeight(1),
                      width: ScreenUtil().setWidth(682),
                    )
                  ],
                )),

            Container(
                padding: EdgeInsets.fromLTRB(8.0, 8.0, 0.0, 0.0),
                child: Column(
                  children: <Widget>[
                    Row(
                      children: <Widget>[
                        Expanded(
                          child: Text(
                            '风险提示',
                            style: TextStyles.mineTs5,
                          ),
                          flex: 1,
                        ),
                        Container(
                          margin: EdgeInsets.fromLTRB(0.0, 0.0, 8.0, 0),
                          child: Image.asset('resource/images/icon_right.png',
                              width: ScreenUtil().setWidth(30),
                              height: ScreenUtil().setHeight(40)),
                        )
                      ],
                    ),
                    Container(
                      margin: EdgeInsets.fromLTRB(0.0, 8.0, 2.0, 0.0),
                      color: Colours.bgec,
                      height: ScreenUtil().setHeight(1),
                      width: ScreenUtil().setWidth(682),
                    )
                  ],
                )),


            Container(
                padding: EdgeInsets.fromLTRB(8.0, 8.0, 0.0, 0.0),
                child: Column(
                  children: <Widget>[
                    Row(
                      children: <Widget>[
                        Expanded(
                          child: Text(
                            '服务协议',
                            style: TextStyles.mineTs5,
                          ),
                          flex: 1,
                        ),
                        Container(
                          margin: EdgeInsets.fromLTRB(0.0, 0.0, 8.0, 0),
                          child: Image.asset('resource/images/icon_right.png',
                              width: ScreenUtil().setWidth(30),
                              height: ScreenUtil().setHeight(40)),
                        )
                      ],
                    ),
                    Container(
                      margin: EdgeInsets.fromLTRB(0.0, 8.0, 2.0, 0.0),
                      color: Colours.bgec,
                      height: ScreenUtil().setHeight(1),
                      width: ScreenUtil().setWidth(682),
                    )
                  ],
                )),

            Container(
                padding: EdgeInsets.fromLTRB(8.0, 8.0, 0.0, 0.0),
                child: Column(
                  children: <Widget>[
                    Row(
                      children: <Widget>[
                        Expanded(
                          child: Text(
                            '隐私政策',
                            style: TextStyles.mineTs5,
                          ),
                          flex: 1,
                        ),
                        Container(
                          margin: EdgeInsets.fromLTRB(0.0, 0.0, 8.0, 0),
                          child: Image.asset('resource/images/icon_right.png',
                              width: ScreenUtil().setWidth(30),
                              height: ScreenUtil().setHeight(40)),
                        )
                      ],
                    ),
                    Container(
                      margin: EdgeInsets.fromLTRB(0.0, 8.0, 2.0, 0.0),
                      color: Colours.bgec,
                      height: ScreenUtil().setHeight(1),
                      width: ScreenUtil().setWidth(682),
                    )
                  ],
                )),


            Container(
                padding: EdgeInsets.fromLTRB(8.0, 8.0, 0.0, 0.0),
                child: Column(
                  children: <Widget>[
                    Row(
                      children: <Widget>[
                        Expanded(
                          child: Text(
                            '关于我们',
                            style: TextStyles.mineTs5,
                          ),
                          flex: 1,
                        ),
                        Container(
                          margin: EdgeInsets.fromLTRB(0.0, 0.0, 8.0, 0),
                          child: Image.asset('resource/images/icon_right.png',
                              width: ScreenUtil().setWidth(30),
                              height: ScreenUtil().setHeight(40)),
                        )
                      ],
                    ),
                    Container(
                      margin: EdgeInsets.fromLTRB(0.0, 8.0, 2.0, 0.0),
                      color: Colours.bgec,
                      height: ScreenUtil().setHeight(1),
                      width: ScreenUtil().setWidth(682),
                    )
                  ],
                )),


          ],
        ));
  }

  ///借款管理 四块
  Widget _conWidget() {
    return Container(
      decoration: AppCardStyle.decoration,
      margin: EdgeInsets.fromLTRB(8.0, 0.0, 8.0, 0.0),
      padding: EdgeInsets.fromLTRB(0.0, 5.0, 0.0, 5.0),
      child: Row(
        children: <Widget>[
          Expanded(
              child: Column(
                children: <Widget>[
                  ClipOval(
                      child: new FadeInImage.assetNetwork(
                    placeholder: 'resource/images/home_marquee.png',
                    image:
                        "https://ss0.baidu.com/6ONWsjip0QIZ8tyhnq/it/u=2182894899,3428535748&fm=58&bpow=445&bpoh=605",
                    width: ScreenUtil().setWidth(100),
                    height: ScreenUtil().setHeight(100),
                    fit: BoxFit.fill,
                  )),
                  Container(
                    margin: EdgeInsets.fromLTRB(0.0, 8.0, 0.0, 0.0),
                    child: Text(
                      '借款管理',
                      style: TextStyles.mineTs4,
                    ),
                  )
                ],
              ),
              flex: 1),
          Expanded(
              child: Column(
                children: <Widget>[
                  ClipOval(
                      child: new FadeInImage.assetNetwork(
                    placeholder: 'resource/images/home_marquee.png',
                    image:
                        "https://ss0.baidu.com/6ONWsjip0QIZ8tyhnq/it/u=55215630,172733418&fm=58&bpow=700&bpoh=998",
                    width: ScreenUtil().setWidth(100),
                    height: ScreenUtil().setHeight(100),
                    fit: BoxFit.fill,
                  )),
                  Container(
                    margin: EdgeInsets.fromLTRB(0.0, 8.0, 0.0, 0.0),
                    child: Text(
                      '资产管理',
                      style: TextStyles.mineTs4,
                    ),
                  )
                ],
              ),
              flex: 1),
          Expanded(
              child: InkWell(
                onTap: (){

                  Application.router.navigateTo(context, Routes.accountInfor);
                //  print('12312312312312');
                },
                child: Column(
                  children: <Widget>[
                    ClipOval(
                        child: new FadeInImage.assetNetwork(
                          placeholder: 'resource/images/home_marquee.png',
                          image:
                          "https://ss0.baidu.com/6ONWsjip0QIZ8tyhnq/it/u=1696383840,109264922&fm=58&bpow=1232&bpoh=1751",
                          width: ScreenUtil().setWidth(100),
                          height: ScreenUtil().setHeight(100),
                          fit: BoxFit.fill,
                        )),
                    Container(
                      margin: EdgeInsets.only(top: 8.0),
                      child: Text(
                        '账号信息',
                        style: TextStyles.mineTs4,
                      ),
                    )
                  ],
                ),
              ),
              flex: 1),
          Expanded(
              child: Column(
                children: <Widget>[
                  ClipOval(
                      child: new FadeInImage.assetNetwork(
                    placeholder: 'resource/images/home_marquee.png',
                    image:
                        "https://ss1.baidu.com/6ONXsjip0QIZ8tyhnq/it/u=282609871,2046421540&fm=58&bpow=2048&bpoh=2745",
                    width: ScreenUtil().setWidth(100),
                    height: ScreenUtil().setHeight(100),
                    fit: BoxFit.fill,
                  )),
                  Container(
                    margin: EdgeInsets.fromLTRB(0.0, 8.0, 0.0, 0.0),
                    child: Text(
                      '还款管理',
                      style: TextStyles.mineTs4,
                    ),
                  ),
                ],
              ),
              flex: 1),
        ],
      ),
    );
  }

  ///借款管理
  Widget _tvWifget() {
    return Container(
      padding: EdgeInsets.all(8),
      child: Row(
        children: <Widget>[
          Container(
            margin: EdgeInsets.fromLTRB(0.0, 0.0, 8.0, 0.0),
            width: ScreenUtil().setWidth(6),
            height: ScreenUtil().setHeight(32),
            color: Colours.baseTextColor,
          ),
          Text(
            '借款管理',
            style: TextStyles.nameTextStyle,
          )
        ],
      ),
    );
  }

  ///申请中的借款.执行中的借款
  Widget _rowLeanMsg2() {
    return Container(
      padding: EdgeInsets.all(8.0),
      child: Row(
        children: <Widget>[
          Expanded(
            child: Container(
              decoration: AppCardStyle.decoration,
              padding: EdgeInsets.all(8.0),
              margin: EdgeInsets.fromLTRB(0.0, 0.0, 4.0, 0.0),
              child: Row(
                children: <Widget>[
                  Expanded(
                    flex: 1,
                    child: Text('申请中的借款', style: TextStyles.mineTs1),
                  ),
                  Text(_rowTv4, style: TextStyles.mineTs3),
                ],
              ),
            ),
          ),
          Expanded(
            child: Container(
              decoration: AppCardStyle.decoration,
              padding: EdgeInsets.all(8.0),
              margin: EdgeInsets.fromLTRB(4.0, 0.0, 0.0, 0.0),
              child: Row(
                children: <Widget>[
                  Expanded(
                    flex: 1,
                    child: Text('申请中的借款', style: TextStyles.mineTs1),
                  ),
                  Text(_rowTv5, style: TextStyles.mineTs3),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }

  ///可用余额等..
  Widget _rowLeadMsg1() {
    return Container(
      decoration: AppCardStyle.decoration,
      margin: EdgeInsets.fromLTRB(8.0, 8.0, 8.0, 0.0),
      padding: EdgeInsets.fromLTRB(0.0, 10.0, 0.0, 10.0),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        children: <Widget>[
          Expanded(
            child: Column(
              children: <Widget>[
                Text(_rowTv1, style: TextStyles.mineTs2),
                Text('可用余额', style: TextStyles.mineTs1),
              ],
            ),
            flex: 1,
          ),
          Expanded(
            child: Column(
              children: <Widget>[
                Text(_rowTv2, style: TextStyles.mineTs2),
                Text('最近需还款', style: TextStyles.mineTs1),
              ],
            ),
            flex: 1,
          ),
          Expanded(
            child: Column(
              children: <Widget>[
                Text(_rowTv3, style: TextStyles.mineTs2),
                Text('距离还款日期', style: TextStyles.mineTs1),
              ],
            ),
            flex: 1,
          ),
        ],
      ),
    );
  }

  ///头像 昵称 id
  Widget _rowHeadAndName() {
    return GestureDetector(
        onTap: _handleHeadOnTap,
        child: Container(
          margin: EdgeInsets.fromLTRB(30, 10, 30, 10),
          child: Row(
            children: <Widget>[
              ClipOval(
                  child: new FadeInImage.assetNetwork(
                placeholder: 'resource/images/home_marquee.png',
                image: _userPicUrl,
                width: ScreenUtil().setWidth(92),
                height: ScreenUtil().setHeight(92),
                fit: BoxFit.fill,
              )),
              Container(
                margin: EdgeInsets.fromLTRB(10, 0.0, 0, 0),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Text(
                      _userName,
                      style: TextStyle(
                          fontSize: ScreenUtil().setSp(32),
                          fontFamily: AppFontFamily.PingFangHeavy,
                          color: Colours.tvBlack),
                    ),
                    Text(
                      _userId,
                      style: TextStyle(
                          fontFamily: AppFontFamily.PingFangMedium,
                          fontSize: ScreenUtil().setSp(24),
                          color: Colours.baseTextColor),
                    )
                  ],
                ),
              )
            ],
          ),
        ));
  }

  ///标题
  Widget _titleBar() {
    return AppBar(
      title: Text(
        '我的',
        style: TextStyle(
            color: Colours.tvBlack,
            fontFamily: AppFontFamily.PingFangBlod,
            fontSize: ScreenUtil().setSp(38)),
      ),
      centerTitle: true,
      backgroundColor: Colours.white,
    );
  }

  ///点击头像区域
  void _handleHeadOnTap() async {
    final result = await Application.router.navigateTo(context, Routes.login);
    if (result is int) {
      if (result == StateCode.code00) {
        _getNetData();
      }
    }

/*    Navigator.push(
        context, MaterialPageRoute(builder: (context) => LoginPage()));*/
  }

  void _setData(UserInforEntity userInforEntity) {
    /*  Map dataStr = SpUtil.getObject(SpKeys.userInfor);
    UserInforEntity entity =
    dataStr == null ? null : UserInforEntity.fromJson(dataStr);*/
    if (userInforEntity != null) {
      _userName = userInforEntity.retObject.name;
      _userId = 'ID:' + userInforEntity.retObject.id;
      _userPicUrl = userInforEntity.retObject.headPic;
      _rowTv1 = userInforEntity.retObject.balance == null
          ? '0元'
          : userInforEntity.retObject.balance + '元';

      _rowTv2 = userInforEntity.retObject.repaymentSum == null
          ? '0元'
          : userInforEntity.retObject.repaymentSum + '元';
      _rowTv3 =
          '${userInforEntity.retObject.repaymentDay == null ? 0 : userInforEntity.retObject.repaymentDay}天';

      _rowTv4 =
          '${userInforEntity.retObject.approving == null ? 0 : userInforEntity.retObject.approving}笔';
      _rowTv5 =
          '${userInforEntity.retObject.repaying == null ? 0 : userInforEntity.retObject.repaying}笔';
    }
  }

  ///获取网络数据
  void _getNetData() async {
    var response = await NetUtils.getWithToken(UmeApi.MINE_GET);
    print('我的相关;$response');

    var data = json.decode(response.toString());

    UserInforEntity userInforEntity = UserInforEntity.fromJson(data);
    SpUtil.putObject(SpKeys.userInfor, userInforEntity);

    _setData(userInforEntity);
  }
}
